import time
import jwt
import datetime
from subsidy.app.config.Config import *
def generate_jwt(data):
    payload = {
        'user_id': data["id"],
        'data': data
        # 'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=8)  # 设置过期时间为 1 小时后
    }
    return jwt.encode(payload, secret_key, algorithm='HS256')

def parse_jwt(jwt_token):
    try:
        _payload = jwt.decode(jwt_token, secret_key, algorithms=['HS256'])
    except jwt.PyJWTError:
        print('token解析失败', jwt_token)
        return False, None
    # else:
    #     print(_payload)
    #     exp = int(_payload["exp"])
    #     if time.time() > exp:
    #         print('已失效')
    #         return False, None

    return True, _payload

if __name__ == '__main__':
    l = parse_jwt("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiYjBlN2Q1YTUtMzYyMi00MzkyLWJhYWYtZjNhNDE4NTk0MTY2IiwiZGF0YSI6eyJpZCI6ImIwZTdkNWE1LTM2MjItNDM5Mi1iYWFmLWYzYTQxODU5NDE2NiIsIm5pY2tuYW1lIjoiIiwidXNlcm5hbWUiOiJnd2wiLCJwYXNzd29yZCI6IjU5MzQ2NjJkZWIzYzIxNDBiYjhlMTY1NjM1MzkyY2I4IiwiZGVsIjowLCJpc19hY3RpdmUiOjAsImlzX3N1cGVydXNlciI6MCwiY3JlYXRlZF9hdCI6IjIwMjQtMTAtMTQgMTA6NTg6MDYiLCJ1cGRhdGVkX2F0IjoiMjAyNC0xMC0xNCAxMDo1ODowNiIsImxhc3RfbG9naW4iOiIyMDI0LTEwLTE0IDEwOjU4OjA2IiwibG9nb3V0X2F0IjpudWxsfX0.cjn-o2lDed0-_mubCDf2xqrzwDvSRXdEr67ciSKoRCM")
    print(l)